package com.mycompany.tableOpe.service;

import com.mycompany.mvc.utils.BaseResult;
import com.mycompany.mvc.utils.EasyResult;
import com.mycompany.mvc.utils.SystemConstant;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.tableOpe.dao.TableOpeDao;
import com.mycompany.tableOpe.entity.TableOpeBean;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/1 0001.
 */

@Service("TableOpeService")
public class TableOpeServiceImpl implements TableOpeService {
    @Autowired
    private TableOpeDao tableOpeDao;


    @Override
    public BaseResult addTable(TableOpeBean tableOpeBean) {
        BaseResult baseResult = new BaseResult();

        try {
            tableOpeDao.addTable(tableOpeBean);
            baseResult.setSuccess(true);
            baseResult.setCode(SystemConstant.returnSuc);
            baseResult.setMsg(SystemConstant.returnSucMsg);
        } catch (Exception e) {
            baseResult.setSuccess(false);
            baseResult.setCode(SystemConstant.returnFal);
            baseResult.setMsg(SystemConstant.returnFalMsg+":系统异常-"+e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }

    @Override
    public BaseResult updateTable(TableOpeBean tableOpeBean) {
        BaseResult baseResult = new BaseResult();

        try {
            tableOpeDao.updateTable(tableOpeBean);
            baseResult.setSuccess(true);
            baseResult.setCode(SystemConstant.returnSuc);
            baseResult.setMsg(SystemConstant.returnSucMsg);
        } catch (Exception e) {
            baseResult.setSuccess(false);
            baseResult.setCode(SystemConstant.returnFal);
            baseResult.setMsg(SystemConstant.returnFalMsg+":系统异常-"+e.getMessage());
            e.printStackTrace();
        }

        return baseResult;
    }

    @Override
    public BaseResult getAllTable() {
        BaseResult baseResult = new BaseResult();

        List<TableOpeBean> allTableList = new ArrayList<TableOpeBean>();

        try {
            allTableList = tableOpeDao.getAllTable();
            baseResult.setSuccess(true);
            baseResult.setCode(SystemConstant.returnSuc);
            baseResult.setMsg(SystemConstant.returnSucMsg);
            /*转化格式*/
            List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
            for (TableOpeBean tableOpeBean:allTableList){
                Map<String,Object> map = new HashMap<String,Object>();
                map = BeanUtils.describe(tableOpeBean);
                mapList.add(map);
            }
            baseResult.setMapList(mapList);
        } catch (Exception e) {
            baseResult.setSuccess(false);
            baseResult.setCode(SystemConstant.returnFal);
            baseResult.setMsg(SystemConstant.returnFalMsg+":系统异常-"+e.getMessage());
            e.printStackTrace();
        }

        return baseResult;
    }

    @Override
    public ModelAndView getSafeItem(Map<String, Object> map) {
        ModelAndView mv = new ModelAndView("/system/safeItem/safeList");
        return mv;
    }

    @Override
    public EasyResult getTableList(Map<String, Object> map) {
        EasyResult easyResult = new EasyResult();
        List<TableOpeBean> list = new ArrayList<>();
        List<TableOpeBean> countList = new ArrayList<>();
        Map<String,Object> countMap = new HashMap<>(map);
        countMap.put("page",null);
        countMap.put("rows",null);

        /*可以由controller层携带参数*/
        try {
            list = tableOpeDao.getAllTableByParam(map);
            countList = tableOpeDao.getAllTableByParam(countMap);
            easyResult.setSuccess(true);
            easyResult.setMsg(SystemConstant.returnSucMsg);
            easyResult.setTotal(countList.size());
            easyResult.setRows(list);
        } catch (Exception e) {
            easyResult.setSuccess(false);
            easyResult.setMsg(SystemConstant.returnFalMsg+":"+e.getMessage());
            e.printStackTrace();
        }

        return  easyResult;
    }

    @Override
    public ModelAndView editTableList(Map<String, Object> map) {
        ModelAndView mv = new ModelAndView("/system/safeItem/safeEdit");

        /*根据ID查询唯一记录*/
        List<TableOpeBean> tableOpeList = tableOpeDao.getAllTableByParam(map);
        TableOpeBean tableOpeBean = CollectionUtils.isNotEmpty(tableOpeList)? tableOpeList.get(0):new TableOpeBean();
        mv.addObject("tableOpeBean",tableOpeBean);

        return mv;
    }

    @Override
    public EasyResult saveTable(TableOpeBean tableOpeBean) {
        EasyResult easyResult = new EasyResult();

        Integer id = tableOpeBean.getId();
        if(SystemUtil.isNull(id) || id.intValue() <= 0){
            easyResult.setSuccess(false);
            easyResult.setMsg(SystemConstant.returnFalMsg+":"+"安全事件Id为空");
        }
        /*可以由controller层携带参数*/
        try {
            tableOpeDao.updateTable(tableOpeBean);
            easyResult.setSuccess(true);
            easyResult.setMsg(SystemConstant.returnSucMsg);
        } catch (Exception e) {
            easyResult.setSuccess(false);
            easyResult.setMsg(SystemConstant.returnFalMsg+":"+e.getMessage());
            e.printStackTrace();
        }

        return easyResult;
    }


}
